package com.archer.utils;

import com.archer.ConfigHolder;
import org.apache.commons.codec.binary.Base64;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import java.security.SecureRandom;

/**
 * Created by YB on 2014/12/1.
 */
public class DesUtil {
    private final static String DES_KEY = ConfigHolder.getConfig().getString("key.des");
    private final static String DES = "DES";

    /**
     * 默认加密
     *
     * @param source
     * @return
     */
    public static String encrypt(String source) {
        try {
            byte[] b = encrypt(source.getBytes(), DES_KEY.getBytes());
            return Base64.encodeBase64String(b);
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
    }

    /**
     * 默认解密
     *
     * @param source
     * @return
     */
    public static String decrypt(String source) {
        try {
            byte[] b = Base64.decodeBase64(source);
            byte[] b1 = decrypt(b, DES_KEY.getBytes());
            return new String(b1);
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
    }

    public static String encrypt(String source, String key) {
        try {
            byte[] b = encrypt(source.getBytes(), key.getBytes());
            return new String(b);
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
    }

    public static String decrypt(String source, String key) {
        try {
            byte[] b = decrypt(source.getBytes(), key.getBytes());
            return new String(b);
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
    }

    public static byte[] encrypt(byte[] data, byte[] key) throws Exception {
        // 生成一个可信任的随机数源
        SecureRandom sr = new SecureRandom();

        // 从原始密钥数据创建DESKeySpec对象
        DESKeySpec dks = new DESKeySpec(key);

        // 创建一个密钥工厂，然后用它把DESKeySpec转换成SecretKey对象
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);
        SecretKey secureKey = keyFactory.generateSecret(dks);

        // Cipher对象实际完成加密操作
        Cipher cipher = Cipher.getInstance(DES);

        // 用密钥初始化Cipher对象
        cipher.init(Cipher.ENCRYPT_MODE, secureKey, sr);

        return cipher.doFinal(data);
    }

    public static byte[] decrypt(byte[] data, byte[] key) throws Exception {
        // 生成一个可信任的随机数源
        SecureRandom sr = new SecureRandom();

        // 从原始密钥数据创建DESKeySpec对象
        DESKeySpec dks = new DESKeySpec(key);

        // 创建一个密钥工厂，然后用它把DESKeySpec转换成SecretKey对象
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);
        SecretKey secureKey = keyFactory.generateSecret(dks);

        // Cipher对象实际完成解密操作
        Cipher cipher = Cipher.getInstance(DES);

        // 用密钥初始化Cipher对象
        cipher.init(Cipher.DECRYPT_MODE, secureKey, sr);

        return cipher.doFinal(data);
    }
}
